package top.whysu.study.thinkinjava.chapter8.polymorphism.example4;
import static net.mindview.util.Print.print;
public class Shared {
    /**
     * 表示Shared对象的引用数量
     */
    private int refcount = 0;
    /**
     * 用来标记Shared对象的名称
     */
    private static long counter = 0;
    private final long id = counter++;
    public Shared() {
        print("Creating " + this);
    }
    public void addRef() {
        refcount++;
    }
    protected void dispose() {
        // 如果引用数为0，则可以回收掉
        if (--refcount == 0) {
            print("Disposing " + this);
        }
    }
    @Override
    public String toString() {
        return "Shared " + id;
    }
}